<html>
<head>
    <title>mysql管理器</title>
    <script src="layui/layui.js"></script>
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
    <ul class="layui-tab-title">
        <li class="layui-this">初始化</li>
        <li>密码管理</li>
        <li>数据库管理</li>
        <li>关于</li>
    </ul>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
            <div class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">初始化密码</label>
                    <div class="layui-input-inline">
                        <input type="password" name="password" required placeholder="请输入密码"
                               autocomplete="off" class="layui-input" id="initpass">
                    </div>
                    <div class="layui-form-mid layui-word-aux">初始化账号为root</div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-primary layui-border-blue" onclick="OnInitMysql()">初始化mysql
                        </button>
                    </div>
                </div>
            </div>
            <br>
            <br>
            <hr class="layui-border-blue">
            <div style="margin: 10px">
                <label>常规功能</label>
                <br>
                <br>
                <button class="layui-btn layui-btn-normal" onclick="StartMySql()">启动mysql</button>
                <button class="layui-btn layui-btn-normal" onclick="StopMysql()">停止mysql</button>
                <button class="layui-btn layui-btn-normal" onclick="CleanMysql()">卸载mysql</button>
            </div>
        </div>
        <div class="layui-tab-item">
            <div class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">老密码</label>
                    <div class="layui-input-block">
                        <input type="password" id="oldpass" placeholder="请输入老密码"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">新密码</label>
                    <div class="layui-input-block">
                        <input type="password" id="newpass" placeholder="请输入新密码"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" onclick="ChangeMysql()">重置密码</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-tab-item">
            <div class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="password" id="datapass" placeholder="请输入密码"
                               autocomplete="on" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" onclick="GetDataBases()">获取所有数据库</button>
                        <button class="layui-btn layui-btn-normal" onclick="NewDatabase()">新建数据库</button>
                    </div>
                </div>
            </div>
            <div style="margin: 10px">
                <div id="showtable"></div>
            </div>
        </div>
        <div class="layui-tab-item">
            <div class="layui-card">
                <div class="layui-card-header">mysql信息</div>
                <div class="layui-card-body" id="mysql_info">
                </div>
            </div>
            <div class="layui-card">
                <div class="layui-card-header">作者信息</div>
                <div class="layui-card-body">
                    丁丁(dinglz) QQ:1149978856
                </div>
            </div>
            <div class="layui-card">
                <div class="layui-card-header">本地mysql地址</div>
                <div class="layui-card-body" id="mysql_path">
                </div>
            </div>
            <div style="margin: 10px">
                <button class="layui-btn layui-btn-primary layui-border-blue" onclick="AddGroup()">加群聊天</button>
            </div>
        </div>
    </div>
</div>
<script id="databasestem" type="text/html">
    <table class="layui-table">
        <tbody>
        {{# layui.each(d.list, function(index, item){ }}
        <tr>
            <td>{{ item }}</td>
            <td>
                <button class="layui-btn layui-btn-sm dodatabases" data-id="{{ item }}">操作</button>
            </td>
        </tr>
        {{# }); }}
        </tbody>
    </table>
</script>
<script>
    window.go.main.App.HasMysql().then(res => {
        if (!res) {
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.alert("未发现本地mysql!\n请将mysql解压并重命名为mysql后放置本地目录下！", {
                    icon: 2,
                    closeBtn: 0,
                    title: "错误！",
                }, function (index) {
                    window.go.main.App.ExitApp()
                    layer.close(index)
                })
            })
        }
    })
    window.go.main.App.GetMysqlPath().then(res => {
        document.getElementById("mysql_path").textContent = res
    })
    window.go.main.App.GetMySqlInfo().then(result => {
        document.getElementById("mysql_info").textContent = result
    });
</script>
<script>
    function AddGroup() {
        window.go.main.App.ToAddGroup()
    }

    function GetDataBases() {
        window.go.main.App.GetDatabases(document.getElementById("datapass").value).then(res => {
            layui.use('laytpl', function () {
                var laytpl = layui.laytpl;
                var data = {"list": res};
                var gettql = document.getElementById("databasestem").innerHTML;
                laytpl(gettql).render(data, function (html) {
                    document.getElementById("showtable").innerHTML = html;
                    layui.use('dropdown', function () {
                        var dropdown = layui.dropdown, $ = layui.jquery;
                        dropdown.render({
                            elem: '.dodatabases'
                            , data: [{
                                title: '删除'
                                , id: '1'
                            }]
                            , click: function (data, othis) {
                                var elem = $(this.elem)
                                    , listId = elem.data('id'); //表格列表的预埋数据
                                if (data.id == 1) {
                                    layer.confirm('是否删除数据库：' + listId + '?', {
                                        icon: 3,
                                        title: '删除数据库',
                                        shadeClose: true,
                                    }, function (index) {
                                        //do something
                                        window.go.main.App.DeleteDatabase(document.getElementById("datapass").value, listId).then((ok) => {
                                            if (!ok) {
                                                window.go.main.App.GetErorr().then(res => {
                                                    layer.alert(res, {
                                                        icon: 2,
                                                        shadeClose: true,
                                                        title: "删除失败！"
                                                    })
                                                })
                                            } else {
                                                layer.alert("删除成功！", {
                                                    icon: 1,
                                                    shadeClose: true,
                                                    title: "删除数据库",
                                                })
                                                GetDataBases()
                                            }
                                        })
                                        layer.close(index);
                                    });
                                }
                            }
                        });
                    });
                });
            });
        })
    }

    function NewDatabase() {

        layui.use('layer', function () {
            var layer = layui.layer;
            if (document.getElementById("datapass").value == "") {
                layer.alert("密码不能为空！", {
                    icon: 2,
                    shadeClose: true,
                    title: "新建数据库失败！"
                })
                return
            }
            layer.prompt({title: "请输入新建的数据库名称"}, function (value, index, elem) {
                if (value == "") {
                    layer.alert("数据库名不能为空！", {
                        icon: 2,
                        shadeClose: true,
                        title: "新建数据库失败！"
                    })
                    return
                }
                window.go.main.App.NewDatabase(value, document.getElementById("datapass").value).then((ok) => {
                    if (!ok) {
                        window.go.main.App.GetErorr().then(res => {
                            layer.alert(res, {
                                icon: 2,
                                shadeClose: true,
                                title: "新建数据库失败！"
                            })
                        })
                    } else {
                        layer.alert("新建数据库成功！", {
                            icon: 1,
                            shadeClose: true,
                            title: "新建",
                        })
                        GetDataBases()
                    }
                })
                layer.close(index);
            });
        })
    }

    function OnInitMysql() {
        layui.use('layer', function () {
            var layer = layui.layer;
            var password = document.getElementById("initpass").value;
            window.go.main.App.InitMysql(password).then((ok) => {
                if (!ok) {
                    window.go.main.App.GetErorr().then(res => {
                        layer.alert(res, {
                            icon: 2,
                            shadeClose: true,
                            title: "初始化失败！"
                        })
                    })
                } else {
                    layer.alert("初始化并启动成功！", {
                        icon: 1,
                        shadeClose: true,
                        title: "初始化",
                    })
                }
            })
        });
    }

    function StartMySql() {
        layui.use('layer', function () {
            var layer = layui.layer;
            window.go.main.App.StartMysql().then(res => {
                if (!res) {
                    window.go.main.App.GetErorr().then(ok => {
                        layer.alert(ok, {
                            icon: 2,
                            shadeClose: true,
                            title: "启动失败！"
                        })
                    })
                } else {
                    layer.alert("启动成功！", {
                        icon: 1,
                        shadeClose: true,
                        title: "启动",
                    })
                }
            })
        });
    }

    function StopMysql() {
        layui.use('layer', function () {
            var layer = layui.layer;
            window.go.main.App.StopMysql().then(res => {
                if (!res) {
                    window.go.main.App.GetErorr().then(ok => {
                        layer.alert(ok, {
                            icon: 2,
                            shadeClose: true,
                            title: "关闭失败！"
                        })
                    })
                } else {
                    layer.alert("关闭成功！", {
                        icon: 1,
                        shadeClose: true,
                        title: "关闭",
                    })
                }
            })
        })
    }

    function CleanMysql() {
        layui.use('layer', function () {
            var layer = layui.layer;
            window.go.main.App.CleanMysql().then(res => {
                if (!res) {
                    window.go.main.App.GetErorr().then(ok => {
                        layer.alert(ok, {
                            icon: 2,
                            shadeClose: true,
                            title: "卸载失败！"
                        })
                    })
                } else {
                    layer.alert("卸载成功！", {
                        icon: 1,
                        shadeClose: true,
                        title: "卸载",
                    })
                }
            })
        })
    }

    function ChangeMysql() {
        layui.use('layer', function () {
            var layer = layui.layer;
            window.go.main.App.ChangeMysql(document.getElementById("oldpass").value, document.getElementById("newpass").value).then(res => {
                if (!res) {
                    window.go.main.App.GetErorr().then(ok => {
                        layer.alert(ok, {
                            icon: 2,
                            shadeClose: true,
                            title: "重置密码失败！"
                        })
                    })
                } else {
                    layer.alert("重置成功！", {
                        icon: 1,
                        shadeClose: true,
                        title: "重置密码",
                    })
                }
            })
        })
    }
</script>
</body>
</html>